let MS = require('./ms.js')
const request = require('./request.js')
const Error = require('./error')
const storage = require('./storage.js')

const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType.js')

const employee = class employee {
    constructor(){}

    get(id) {
        let params = {
            wxUserId: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EMPLOYEE.GET_EMPLOYEE.url,
                MS._config.parameters.EMPLOYEE.GET_EMPLOYEE.method, params).then(res => {
                    storage.save('employee', res.data)
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    saveQRCodeUrl(id, qrCodeUrl) {
        let params = {
            id: id,
            qrCodeUrl: qrCodeUrl
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EMPLOYEE.SAVE_QR_CODE_URL.url,
                MS._config.parameters.EMPLOYEE.SAVE_QR_CODE_URL.method, params).then(res => {
                    resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    createWorkTraces(options) {
        let params = {}
        if(!isUndefined(options)) {
            if(!isUndefined(options.employeeId) && isString(options.employeeId)) {
                params.employeeId = options.employeeId;
            }
            if(!isUndefined(options.remark) && isString(options.remark)) {
                params.remark = options.remark;
            }
            if(!isUndefined(options.longitude) && isNumber(options.longitude)) {
                params.longitude = options.longitude;
            }
            if(!isUndefined(options.latitude) && isNumber(options.latitude)) {
                params.latitude = options.latitude;
            }
            if(!isUndefined(options.picUrl) && isString(options.picUrl)) {
                params.picUrl = options.picUrl;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EMPLOYEE.CREATE_WORK_TRACES.url,
                MS._config.parameters.EMPLOYEE.CREATE_WORK_TRACES.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadWorkTraces(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options)) {
            if(!isUndefined(options.employeeId) && isString(options.employeeId)) {
                params.employeeId = options.employeeId;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.EMPLOYEE.LOAD_WORK_TRACES.url,
                MS._config.parameters.EMPLOYEE.LOAD_WORK_TRACES.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = employee